//
//  ViewController.m
//  weatherAPI
//
//  Created by 楼楚 on 16/3/1.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSString *httpUrl = @"http://apis.baidu.com/heweather/weather/free";
    NSString *httpArg = @"city=beijing";
    [self request: httpUrl withHttpArg: httpArg];

}
-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)HttpArg  {
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, HttpArg];
    NSURL *url = [NSURL URLWithString: urlStr];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    [request setHTTPMethod: @"GET"];
    [request addValue: @"yourapikey" forHTTPHeaderField: @"apikey"];
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//
                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   NSLog(@"HttpResponseBody %@",responseString);
                                   NSData *newdata = [responseString dataUsingEncoding:NSUTF8StringEncoding];
                                   NSError *err;
                                   NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:newdata options:NSJSONReadingMutableContainers error:&err];
                                   NSArray *arr = [dic objectForKey:@"HeWeather data service 3.0"];
                                   NSDictionary *dic1 = arr[0];
                                   NSArray *arr1 = [dic1 objectForKey:@"hourly_forecast"];
                                   NSDictionary *dic2 = arr1[0];
                                   NSString *te = [dic1 objectForKey:@"status"];
                                   NSLog(@"%@",te);
                                   //                                   NSString *str = [test objectForKey:@"status"];
//                                   NSLog(@"-----%@",str);
                                   
                                   //                                   NSLog(@"%@",[dic objectForKey:@"pres"]);
                               }
                                   
                               }];
    
}


//+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
//    if (jsonString == nil) {
//        return nil;
//    }
//    
//    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
//    NSError *err;
//    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
//                                                        options:NSJSONReadingMutableContainers
//                                                          error:&err];
//    if(err) {
//        NSLog(@"json解析失败：%@",err);
//        return nil;
//    }
//    return dic;
//}
//

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
